package de.global42.clubapp.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
/**
 * @author lutz
 *
 */
@Entity
public class Venue implements Serializable{

   /**
	 * Generated Version UID
	 */
	private static final long serialVersionUID = 4017636169668717260L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   protected Long id = null;
   
   @Version
   @Column(name = "version")
   protected int version = 0;
   
   @Column
   @NotNull
   protected String name;
   
   @Embedded
   @NotNull
   protected Address address;
   
   @Embedded
   protected ContactInfo contactInfo;

   @Embedded 
   protected GeoLocation geoLocation;

   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(	name = "venue_mission",
		   		joinColumns = 	@JoinColumn(name = "venue_id",referencedColumnName = "id"),
				inverseJoinColumns =	@JoinColumn(name = "mission_id", referencedColumnName = "id"))
   protected Set<Mission> missions = new HashSet<Mission>();
   
   @Temporal(TemporalType.TIMESTAMP)
   protected Date missionsUpdatedOn;
   
   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(	name = "venue_coupon",
		   		joinColumns = 	@JoinColumn(name = "venue_id",referencedColumnName = "id"),
				inverseJoinColumns =	@JoinColumn(name = "coupon_id", referencedColumnName = "id"))
   protected Set<Coupon> coupons;
   
   public Long getId()
   {
      return this.id;
   }

   public void setId(final Long id)
   {
      this.id = id;
   }

   public int getVersion()
   {
      return this.version;
   }

   public void setVersion(final int version)
   {
      this.version = version;
   }

   public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public Address getAddress() {
	return address;
}

public void setAddress(Address address) {
	this.address = address;
}

public ContactInfo getContactInfo() {
	return contactInfo;
}

public void setContactInfo(ContactInfo contactInfo) {
	this.contactInfo = contactInfo;
}

public GeoLocation getGeoLocation() {
	return geoLocation;
}

public void setGeoLocation(GeoLocation geoLocation) {
	this.geoLocation = geoLocation;
}

public Set<Mission> getMissions() {
	return missions;
}

public void setMissions(Set<Mission> missions) {
	this.missions = missions;
}

public Set<Coupon> getCoupons() {
	return coupons;
}

public void setCoupons(Set<Coupon> coupons) {
	this.coupons = coupons;
}

public Date getMissionsUpdatedOn() {
	return missionsUpdatedOn;
}

public void setMissionsUpdatedOn(Date missionsUpdatedOn) {
	this.missionsUpdatedOn = missionsUpdatedOn;
}

public String toString()
   {
      String result = "";
      if (id != null)
         result += id;
      return result;
   }

   @Override
   public boolean equals(Object that)
   {
      if (this == that)
      {
         return true;
      }
      if (that == null)
      {
         return false;
      }
      if (getClass() != that.getClass())
      {
         return false;
      }
      if (id != null)
      {
         return id.equals(((Venue) that).id);
      }
      return super.equals(that);
   }

   @Override
   public int hashCode()
   {
      if (id != null)
      {
         return id.hashCode();
      }
      return super.hashCode();
   }
}